package cn.cms.util;

import com.codahale.metrics.MetricRegistry;
import com.izettle.metrics.influxdb.InfluxDbHttpSender;
import com.izettle.metrics.influxdb.InfluxDbReporter;
import com.izettle.metrics.influxdb.InfluxDbSender;
import java.util.concurrent.TimeUnit;

/**
 * Created by leon on 2017/3/3.
 */
public class InfluxDbReporterUtil {

    private MetricRegistry metricRegistry;

    private static String protocol = "http";
    private static String hostname;
    private static int port = 8086;
    private static String database;
    private static String authString;
    private static TimeUnit timePrecision = TimeUnit.SECONDS;
    private static int connectTimeout = 3000;
    private static int readTimeout = 3000;
    private static String measurementPrefix = "galaxy";

    public InfluxDbReporterUtil() {


    }

    public InfluxDbReporter build(){
        InfluxDbReporter influxDbReporter = null;
        try {

            InfluxDbSender influxDbSender = new InfluxDbHttpSender(protocol, hostname, port, database, authString, timePrecision, connectTimeout, readTimeout, measurementPrefix);
            influxDbReporter = InfluxDbReporter.forRegistry(metricRegistry).
                    convertRatesTo(TimeUnit.SECONDS).
                    convertDurationsTo(TimeUnit.MILLISECONDS)
                    .build(influxDbSender);
            influxDbReporter.start(1, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return influxDbReporter;
    }

    public String getAuthString() {
        return authString;
    }

    public void setAuthString(String authString) {
        this.authString = authString;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public TimeUnit getTimePrecision() {
        return timePrecision;
    }

    public void setTimePrecision(TimeUnit timePrecision) {
        this.timePrecision = timePrecision;
    }

    public static String getMeasurementPrefix() {
        return measurementPrefix;
    }

    public static void setMeasurementPrefix(String measurementPrefix) {
        InfluxDbReporterUtil.measurementPrefix = measurementPrefix;
    }


}
